<?php

include '../config/db_config.php';
include '../lib/kelola_kategori.php';
include '../lib/kelola_produk.php';
include '../lib/kelola_gambar.php';

session_start();
if(!isset($_SESSION)){
	header('location:login.php');
}

$id_produk = $_GET['id'];

$kelola_kategori = new kelola_kategori();
$kelola_produk = new kelola_produk();
$kelola_gambar = new kelola_gambar();

$all_kategori = $kelola_kategori->get_all_kategori();

$res = $kelola_produk->select_produk($id_produk);

$produk = mysql_fetch_array($res);

$res = $kelola_gambar->select_gambar($id_produk);

$gambar = array();
while($row = mysql_fetch_assoc($res)){
	$gambar[] = $row;
}

//var_dump($gambar);

?>

<html>
  <head>
    <title>Smart Commerce</title>
    <link href="../css/bootstrap.min.css" rel="stylesheet">
	<style type="text/css">
      body {
        padding-top: 60px;
        padding-bottom: 40px;
      }
    </style>
    <link href="../css/bootstrap-responsive.min.css" rel="stylesheet">
	<script src="../js/jquery-1.7.2.min.js"></script>
	<script src="../js/bootstrap.min.js"></script>
	<script src="../js/autoNumeric-1.7.5.js"></script>
</head>
<body>
	<div class="navbar navbar-inverse navbar-fixed-top">
		<div class="navbar-inner">
			<div class="container">
			<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
			</a>
			
			<a class="brand" href="home_technopreneur.php">Beranda</a>
			<a class="brand">Tentang Kami</a>
			<a class="brand">Kontak</a>
			
				<div class="nav-collapse collapse">
				<form class="navbar-form pull-right" method="post">
				</form>
			<form class="navbar-form pull-right" method="post">
				<a href="../logout.php" class="btn btn-small btn-primary pull-right">Logout</a>
				<a href="edit_profil.php" class="btn btn-small btn-primary pull-right">Edit Profil</a>
			</form>
				
			    </div>
			</div>
		</div>
    </div>
	
	<div class="container">
		<div class="hero-unit"style="background-image:url('../images/header.jpg');" >
			<h1>Smart Commerce</h1>
			<blockquote class="pull-right"> 
				<p>Website khusus Technopreneur Surabaya</p>
			</blockquote>
		</div>
		
		<ul class="breadcrumb">
			<li><a href="home_technopreneur.php">Home</a> <span class="divider">/</span></li>
			<li class="active">Tambah Produk</li>
		</ul>
		
		<form name="input" action="../edit_produk.php" method="post" enctype="multipart/form-data">
		<div class="control-group">
			<label class="control-label" for="inputUser">Nama Produk</label>
			<div class="controls">
				<input style="width:400px; height:30px" type="text" name="nama_produk" placeholder="Nama Produk" autofocus="autofocus" autocomplete="off" maxlength="32" required="required" value="<?php echo $produk['NAMA_PRODUK']; ?>" />
			</div>
		</div>
		<div class="control-group">
			<label class="control-label" for="inputUser">Kategori Produk</label>
			<div class="controls">
				<select name="id_kategori">
					<?php
					foreach($all_kategori as $kategori){
						echo '<option value="'.$kategori['ID_KATEGORI'].'">'.$kategori['NAMA_KATEGORI'].'</option>';
					}
					?>
				</select>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label" for="inputPassword">Harga</label>
			<div class="controls">
				<div class="input-prepend input-append">
				  <span class="add-on">Rp.</span>
				  <input id="harga" class="span2" name="harga" style="width:300px;height:30px;text-align:right;" min="1" required value="" />
				</div>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label" for="inputPassword">Deskripsi Produk</label>
			<div class="controls">
				<textarea name="deskripsi" rows="7" style="width:400px;" type="text" name="password" placeholder="" maxlength="1024" required="required"><?php echo $produk['DESKRIPSI_PRODUK']; ?></textarea>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label" for="inputPassword">Gambar</label>
			<div class="controls">
			<?php
			for($i = 0; $i < count($gambar) ; $i++){
			
				echo '<div class="row">';
				echo '<div class="span2 thumbnail">';
				echo '<img src="../'.$gambar[$i]['URL'].'" style="max-width:250px; height:95px" alt="">';
				echo '<input type="hidden" name="id_gambar'.($i+1).'" value="'.$gambar[$i]['ID_GAMBAR'].'" />';
				echo '</div>';
				echo '<div class="span2">';
				echo '<textarea name="edit_deskripsi_gambar'.($i+1).'" rows="3" style="width:200px;display:block;" type="text" name="password" placeholder="Deskripsi Gambar" maxlength="512">'.$gambar[$i]['DESKRIPSI_GAMBAR'].'</textarea>';
				echo '<a class="btn btn-warning btn-small" onclick="hapus_gambar('.$produk['ID_PRODUK'].','.$gambar[$i]['ID_GAMBAR'].')">Delete</a>';
				echo '</div>';
				echo '</div><br>';
			}
			?>
			</div>
			<br>
			<div class="controls">
				<?php
				for($i = 1; $i < (6 - count($gambar)); $i++){

					echo '<div id="upload'.$i.'" style="display:none;">';
					echo '<input class="input-large" type="file" name="gambar'.$i.'" accept="image/png,image/jpeg,image/jpg" style="display:block;">';
					echo '<textarea name="deskripsi_gambar'.$i.'" rows="1" style="width:400px;display:block;" type="text" name="password" placeholder="Deskripsi Gambar '.$i.'" maxlength="512"></textarea>';
					echo '</div>';
				}
				?>
				<small><span class="help-block">Max 200 kb. jpg / jpeg / png</span></small>
			</div>
		</div>
		<a class="btn btn-mini" id="tambah"><i class="icon-plus"></i> tambah</a></br></br>
		<div class="control-group">
			<div class="controls">
				<input type="hidden" name="id_produk" value="<?php echo $produk['ID_PRODUK']; ?>" />
				<button type="submit" class="btn">Simpan</button> 
			</div>
		</div>
		</form>
		
		
	</div>

	<script>
		$(document).ready(function()
		{
			$('#harga').keyup(function()
			{
				$('#harga').autoNumeric({
					aSep:'.',
					dGroup:'3',
					aDec:',',
					vMax:'999999999999',
					mDec:'0'
				});
				
			});
			
			$('#harga').autoNumeric({
				aSep:'.',
				dGroup:'3',
				aDec:',',
				vMax:'999999999999',
				mDec:'0'
			});
			
			$('#harga').autoNumericSet(<?php echo $produk['HARGA']; ?>);
			
			
			var i = 0;
			$('#tambah').click(function () {
				i++;
				//$('input[name="gambar' + i + '"]').css('display', 'block');
				$('#upload' + i).css('display', 'block');
				if(i == <?php echo (5 - count($gambar)); ?>){
					$('#tambah').css('display','none');
				}
			});
			
		});

		function hapus_gambar(idp,idg){
			var c = confirm("Anda akan menghapus gambar ini beserta deskripsinya. Lanjutkan?");
			if(c){
				window.location="../hapus_gambar.php?idp="+idp+"&idg="+idg;
			}
		}
	</script>
  </body>
</html>